//
//  YYTabbarController.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/7/29.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYTabbarController.h"
#import "YYHomeVC.h"
#import "YYSeraviceVC.h"
#import "YYMineVC.h"
#import "YYInsuranceVC.h"
#import "YYBuyCarVC.h"

@interface YYTabbarController ()

@end

@implementation YYTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setDelegate:self];
    [self setupChildControllers];
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    //改变tabbar 线条颜色
    CGRect rect = CGRectMake(0, 0, DEF_SCREEN_WIDTH(), 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   DEF_COLOR_LINE.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
       //这句表示取消tabBar的透明效果
    [UITabBar appearance].translucent = NO;
}

- (void)setupChildControllers{
    YYHomeVC * home = (YYHomeVC *)[CFUtility getVC:@"YYHomeVC" Storyboard:HOME];
    home.navigationItem.title = @"学车";
    [self addChildVCWith:home title:@"学车" nmlImgName:@"icon_menu_studycar" selImgName:@"icon_menu_studiedcar"index:0];
    YYBuyCarVC * buyCar = (YYBuyCarVC *)[CFUtility getVC:@"YYBuyCarVC" Storyboard:BUYCAR];
    buyCar.navigationItem.title = @"买车";
    [self addChildVCWith:buyCar title:@"买车" nmlImgName:@"icon_menu_buycar" selImgName: @"icon_menu_boughtcar"index:1];
    YYInsuranceVC * vc3 = (YYInsuranceVC *)[CFUtility getVC:@"YYInsuranceVC" Storyboard:INSURANCE];
    vc3.navigationItem.title = @"车险";
    [self addChildVCWith:vc3 title:@"车险" nmlImgName:@"icon_menu_reservation car " selImgName: @"icon_menu_reservatied car "index:2];
//    YYSeraviceVC * seracice = (YYSeraviceVC *)[CFUtility getVC:@"YYSeraviceVC" Storyboard:SERAVICE];
//        seracice.navigationItem.title = @"服务";
//        [self addChildVCWith:seracice title:@"服务" nmlImgName:@"icon_menu_servicecar" selImgName: @"icon_menu_servicedcar"index:3];
    YYMineVC * mine = (YYMineVC *)[CFUtility getVC:@"YYMineVC" Storyboard:MINE];
    mine.navigationItem.title = @"我的";
    [self addChildVCWith:mine title:@"我的" nmlImgName:@"icon_menu_mine" selImgName: @"icon_menu_mined" index:3];
    
}

- (void)addChildVCWith:(UIViewController *)vc title:(NSString *)title nmlImgName:(NSString *)nmlImgName selImgName:(NSString *)selImgName index:(NSInteger)index {
    
    YYNavController *nav = [[YYNavController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    //设置标题
    nav.tabBarItem.title = title;
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            DEF_COLOR_TEXT, NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateNormal];
    
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            DEF_COLOR_MAIN, NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateSelected];
    
    //设置普通状态图片
    nav.tabBarItem.image = [[UIImage imageNamed:nmlImgName]
                            imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selImg = [UIImage imageNamed:selImgName];
    selImg = [selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selImg;
    nav.tabBarItem.tag = index;
    nav.tabBarItem.imageInsets =UIEdgeInsetsMake(0,0, 0,0);
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if (viewController.tabBarItem.tag == 3){
        if (!DEF_UID) {
            [[AppDelegate shareAppDelegate] enterLogin];
            return NO;
        }
    }
    return YES;
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
   // _selIndex = item.tag;
    
}

@end
